﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLL.TrainingCourseware
{
	/// <summary>
	/// 控制类
	/// </summary>
	/// <![CDATA[2014-8-26 11:24:30]]>
	public class Component : DLL.ComponentBase
	{
		#region 属性
		/// <summary>
		/// 问题数据类对象
		/// </summary>
		/// <![CDATA[2014-8-26 11:24:30]]>
		private Database clsDatabase;		// 数据类对象
		/// <summary>
		/// 配置类对象
		/// </summary>
		/// <![CDATA[2014-8-26 11:24:30]]>
		private Config clsConfig = new Config();		// 配置类对象
		#endregion 属性

		#region 构造函数
		/// <summary>
		/// 控制类构造函数
		/// </summary>
		/// <![CDATA[2014-8-26 11:24:30]]>
		public Component(System.Web.UI.Page page)
			: base(page)
		{
			clsDatabase = new Database(page);
			clsData = clsDatabase;

			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nID", clsDatabase.ID, clsDatabase.listStructure[clsDatabase.ID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nStatus", clsDatabase.STATUS, clsDatabase.listStructure[clsDatabase.STATUS].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nTrainingID", clsDatabase.TRAININGID, clsDatabase.listStructure[clsDatabase.TRAININGID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nCoursewareID", clsDatabase.COURSEWAREID, clsDatabase.listStructure[clsDatabase.COURSEWAREID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nNumber", clsDatabase.NUMBER, clsDatabase.listStructure[clsDatabase.NUMBER].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strTrainingName", 0, clsDatabase.clsTrainingDB.listStructure[clsDatabase.clsTrainingDB.NAME].strName, false));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strCoursewareName", 0, clsDatabase.clsCoursewareDB.listStructure[clsDatabase.clsCoursewareDB.NAME].strName, false));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strCoursewarePath", 0, clsDatabase.clsCoursewareDB.listStructure[clsDatabase.clsCoursewareDB.PATH].strName, false));

			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nID", clsDatabase.listStructure[clsDatabase.ID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nStatus", clsDatabase.listStructure[clsDatabase.STATUS].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nTrainingID", clsDatabase.listStructure[clsDatabase.TRAININGID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strTrainingIDs", clsDatabase.listStructure[clsDatabase.TRAININGID].strName, "IN"));
		}
		#endregion 构造函数

		protected override TonyNet4V1.EntityBase DataToEntity(System.Data.DataRow dr)
		{
			Entity clsEntity = new Entity();

			clsEntity = (Entity) base.DataToEntity(dr);
			clsEntity.NumberName = "第" + clsEntity.Number.ToString() + "次课程";

			return clsEntity;
		}

		#region 类型转换
		protected override TonyNet4V1.EntityBase GetEntity()
		{
			return new Entity();
		}
		protected override TonyNet4V1.QueryEntityBase GetQuery()
		{
			return new QueryEntity();
		}
		protected override TonyNet4V1.DataBase GetData()
		{
			return new Database(page);
		}
		/// <summary>
		/// 把查询结果链表中的实体类型转为现在的实体类型
		/// </summary>
		/// <![CDATA[2014-8-26 11:24:30]]>
		/// <param name="list"></param>
		/// <returns></returns>
		private List<Entity> ConvertListEntityType(List<TonyNet4V1.EntityBase> list)
		{
			List<Entity> listEntity = new List<Entity>();

			for (int i = 0; i < list.Count; i++)
			{
				Entity clsEntity = new Entity();

				clsEntity = (Entity)list[i];

				listEntity.Add(clsEntity);
			}

			return listEntity;
		}
		#endregion 类型转换

		#region 查询
		/// <summary>
		/// 根据ID获得实体对象
		/// </summary>
		/// <![CDATA[2014-8-26 11:24:30]]>
		/// <param name="nID"></param>
		/// <returns></returns>
		public new Entity GetOne(int nID)
		{
			return (Entity)base.GetOne(nID);
		}

		/// <summary>
		/// 查询所有
		/// </summary>
		/// <![CDATA[2014-8-26 11:24:30]]>
		/// <returns></returns>
		public new List<Entity> QueryAll()
		{
			return ConvertListEntityType(base.QueryAll());
		}

		/// <summary>
		/// 查询
		/// </summary>
		/// <![CDATA[2014-8-26 11:24:30]]>
		/// <returns></returns>
		public List<Entity> Query(QueryEntity clsQuery)
		{
			return ConvertListEntityType(base.Query(clsQuery));
		}

		#endregion 查询

		/// <summary>
		/// 查询一个培训班所有课件
		/// </summary>
		/// <param name="nTrainingID"></param>
		/// <returns></returns>
		public List<Entity> QueryOneTrainingCourseware(int nTrainingID)
		{
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.nTrainingID = nTrainingID;

			return Query(clsQuery);
		}

		/// <summary>
		/// 查询一个学习者能学习的培训班课件
		/// </summary>
		/// <param name="nUserID">学习者用户ID</param>
		/// <returns>课件实体</returns>
		public List<Entity> QueryOneLearnerTrainingCourseware()
		{
			TrainingPerson.Component clsTrainingPersonComp = new TrainingPerson.Component(page);
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.strTrainingIDs = clsTrainingPersonComp.QueryOneLearnerTraining(this.nUserID);

			return Query(clsQuery);
		}

		/// <summary>
		/// 根据培训班ID查询该培训班所有课程
		/// </summary>
		/// <param name="nTrainingID">培训班ID</param>
		/// <returns>一个培训班所有课程</returns>
		public List<Entity> QueryByTrainingID(int nTrainingID)
		{
			QueryEntity clsQuery = new QueryEntity();
			string strOldOrder = "";
			List<Entity> list = new List<Entity>();

			strOldOrder = clsDatabase._strOrder;
			clsQuery.nTrainingID = nTrainingID;

			clsDatabase._strOrder = " ORDER BY "+clsDatabase.listStructure[clsDatabase.NUMBER].strName+" ASC";
			list = Query(clsQuery);
			clsDatabase._strOrder = strOldOrder;

			return list;
		}
	}
}
